//
//  TravlesViewController.m
//  MemoryTravel
//
//  Created by dllo on 15/12/21.
//  Copyright © 2015年 fcx. All rights reserved.
//

#import "TravlesViewController.h"

@interface TravlesViewController ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIButton *TravelNotesButton;

@property (nonatomic, retain) UIButton *FeaturesButton;

@property (nonatomic, strong) UIView *moveView;

@property (nonatomic, strong) UIView *backgroundView;

@property (nonatomic, strong) LeftTableViewController *leftView;

@property (nonatomic, strong) RightTableViewController *rightView;

@property (nonatomic, strong) UIScrollView *scrollView;

@end

@implementation TravlesViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    self.title = @"旅途";

    UIView *firstView = [UIView new];
    [self.view addSubview:firstView];

    self.backgroundView = [UIView new];
    self.backgroundView.backgroundColor = [UIColor colorWithRed:230 / 255.0 green:230 / 255.0 blue:230 / 255.0 alpha:1];
    self.backgroundView.layer.masksToBounds = YES;//裁掉边界
    self.backgroundView.layer.cornerRadius = 5;//设置半径
    [firstView addSubview:_backgroundView];

#pragma mark -- 游记
    self.TravelNotesButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_TravelNotesButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_TravelNotesButton setTitleColor:[UIColor colorWithRed:58 / 255.0 green:134 / 255.0 blue:217 /255.0 alpha:1] forState:UIControlStateHighlighted];
    [_TravelNotesButton setTitleColor:[UIColor colorWithRed:58 / 255.0 green:134 / 255.0 blue:217 /255.0 alpha:1] forState:UIControlStateSelected];
    _TravelNotesButton.tag = 1001;
    [_TravelNotesButton setTitle:@"游记" forState:UIControlStateNormal];
    [_backgroundView addSubview:_TravelNotesButton];
    [_TravelNotesButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    _TravelNotesButton.selected = YES;

#pragma mark -- 专题
    self.FeaturesButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_FeaturesButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_FeaturesButton setTitleColor:[UIColor colorWithRed:58 / 255.0 green:134 / 255.0 blue:217 /255.0 alpha:1] forState:UIControlStateHighlighted];
    [_FeaturesButton setTitleColor:[UIColor colorWithRed:58 / 255.0 green:134 / 255.0 blue:217 /255.0 alpha:1] forState:UIControlStateSelected];
    _FeaturesButton.tag = 1002;
    [_FeaturesButton setTitle:@"专题" forState:UIControlStateNormal];
    [_FeaturesButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [_backgroundView addSubview:_FeaturesButton];
    _FeaturesButton.selected = NO;

#pragma mark -- 滑块
    self.moveView = [UIView new];
    self.moveView.backgroundColor = [UIColor whiteColor];
    self.moveView.layer.masksToBounds = NO;//裁掉边界
    self.moveView.layer.cornerRadius = 5;//设置半径
    [_backgroundView addSubview:self.moveView];
    
    [_backgroundView bringSubviewToFront:self.TravelNotesButton];
    [_backgroundView bringSubviewToFront:self.FeaturesButton];

#pragma mark -- 底板
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 44, Width, Height)];
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.pagingEnabled = YES;
    self.scrollView.delegate = self;
    self.scrollView.bounces = NO;
    self.scrollView.contentSize = CGSizeMake(Width * 2, Height);
    [self.view addSubview:self.scrollView];

#pragma mark -- 左(游记)
    self.leftView = [LeftTableViewController new];
    [self addChildViewController:self.leftView];
    [self.scrollView addSubview:self.leftView.view];
    self.leftView.view.frame = CGRectMake(0, 0, Width, Height);

#pragma mark -- 右(专题)
    self.rightView = [RightTableViewController new];
    [self addChildViewController:self.rightView];
    [self.scrollView addSubview:self.rightView.view];
    self.rightView.view.frame = CGRectMake(Width, 0, Width, Height);

    [firstView mas_remakeConstraints:^(MASConstraintMaker *make) {

        make.left.equalTo(self.view.mas_left);
        make.top.equalTo(self.view.mas_top);
        make.width.equalTo(self.view.mas_width);
        make.height.mas_equalTo(44);
    }];

    [_backgroundView mas_remakeConstraints:^(MASConstraintMaker *make) {

        make.left.equalTo(firstView.mas_left).offset(5);
        make.top.equalTo(firstView.mas_top).offset(5);
        make.width.equalTo(firstView.mas_width).offset(-10);
        make.height.mas_equalTo(34);
    }];

    [_TravelNotesButton mas_remakeConstraints:^(MASConstraintMaker *make) {

        make.left.and.top.mas_equalTo(1);
        make.bottom.mas_equalTo(-1);
        make.width.equalTo(self.backgroundView.mas_width).multipliedBy(0.5);
    }];

    [_FeaturesButton mas_remakeConstraints:^(MASConstraintMaker *make) {

        make.top.mas_equalTo(1);
        make.right.and.bottom.mas_equalTo(-1);
        make.width.equalTo(self.backgroundView.mas_width).multipliedBy(0.5);
    }];

    [_moveView mas_remakeConstraints:^(MASConstraintMaker *make) {

        make.left.and.top.mas_equalTo(1);
        make.bottom.mas_equalTo(-1);
        make.width.mas_equalTo((Width - 10) / 2.0 - 2);
    }];
}

#pragma mark -- 点击触发方法
- (void)buttonAction:(UIButton *)button{

    if (button.tag == 1001) {

        _TravelNotesButton.selected = YES;

        self.FeaturesButton.selected = NO;

        [_backgroundView bringSubviewToFront:self.TravelNotesButton];

        [_moveView mas_updateConstraints:^(MASConstraintMaker *make) {

            make.left.mas_equalTo(1);
            make.right.mas_equalTo(- ((Width - 10) / 2.0 + 1));
            make.width.mas_equalTo((Width - 10) / 2.0 - 2);
        }];


        [UIView animateWithDuration:0.3 animations:^{


            [self.backgroundView layoutIfNeeded];
            self.scrollView.contentOffset = CGPointMake(0, 0);
        }];
    }

    if (button.tag == 1002) {

        _FeaturesButton.selected = YES;
        self.TravelNotesButton.selected = NO;

        [_backgroundView bringSubviewToFront:self.FeaturesButton];

        [_moveView mas_updateConstraints:^(MASConstraintMaker *make) {

            make.right.mas_equalTo(-1);
            make.left.mas_equalTo((Width - 10) / 2.0 + 1);
        }];

        [UIView animateWithDuration:0.3 animations:^{

            [self.backgroundView layoutIfNeeded];
            self.scrollView.contentOffset = CGPointMake(Width, 0);
        }];
    }
}

#pragma mark -- 滑动触发
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{

    [_moveView mas_updateConstraints:^(MASConstraintMaker *make) {

        make.left.mas_equalTo((scrollView.contentOffset.x / Width) * ((Width - 10) / 2.0) + 1);
        make.right.mas_equalTo(- (1 - (scrollView.contentOffset.x / Width)) * ((Width - 10) / 2.0) - 1);
    }];
}

#pragma mark -- 减速停止触发
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

    if (scrollView.contentOffset.x == 0) {

        self.TravelNotesButton.selected = YES;
        self.FeaturesButton.selected = NO;

        [_moveView mas_updateConstraints:^(MASConstraintMaker *make) {

            make.left.mas_equalTo(1);
            make.right.mas_equalTo(- ((Width - 10) / 2.0 + 1));
        }];
    }

    if (scrollView.contentOffset.x == Width) {

        self.TravelNotesButton.selected = NO;
        self.FeaturesButton.selected = YES;

        [_moveView mas_updateConstraints:^(MASConstraintMaker *make) {
            
            make.right.mas_equalTo(-1);
            make.left.mas_equalTo((Width - 10) / 2.0 + 1);
        }];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
